home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / program / srcbkvt.zip / LISTBOX.ASC < prev    next >
Text File  |  1996-07-08  |  7KB  |  308 lines

  1.  
  2. _Writing Delphi Components_
  3. by William Stamatakis
  4.  
  5. Listing One
  6. { * Unit Name: tbillist.pas
  7.   * Author:    William Stamatakis
  8.   * Note:      Multi-Char Listbox Component (TBillListBox)
  9. }
  10. unit TBillist;
  11.  
  12. interface
  13.  
  14. uses
  15.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  16.   Forms, Dialogs, StdCtrls, FindDlg, Buttons, DsgnIntf, About;
  17. type
  18.   TAboutProperty = class(TPropertyEditor)
  19. public
  20.   procedure Edit; override;
  21.   function GetAttributes: TPropertyAttributes; override;
  22.   function GetValue: string; override;
  23. end;
  24. type TItemsProperty = class(TPropertyEditor)
  25. public
  26.   procedure Edit; override;
  27.   function GetAttributes: TPropertyAttributes; override;
  28.   function GetValue: string; override;
  29. end;
  30. type
  31.   TBillListBox = class(TListBox)
  32.   private
  33.     dlgFind: TdlgFind;
  34.     FAutoSearch: Boolean;
  35.     FAbout: ShortInt;
  36.   protected
  37.     procedure KeyPress(var Key: Char); override;
  38.   public
  39.     procedure FindItemDlg(const SearchItem: string);
  40.     function SearchList(Item: PChar): Integer;
  41.     procedure SetCurrentIndex( CurrIndex: LongInt);
  42.   published
  43.     property AutoSearch: Boolean read FAutoSearch write FAutoSearch
  44.              default False;
  45.     property About: ShortInt read FAbout write FAbout;
  46. end;
  47. var
  48.   CurrentIndex: LongInt;
  49. procedure Register;
  50.  
  51. implementation
  52.  
  53. uses Listdlg;
  54. {TAboutProperty Methods.}
  55. procedure TAboutProperty.Edit;
  56. var
  57.   dlgAbout: TAboutbox;
  58. begin
  59.   dlgAbout := TAboutbox.Create(Application);   dlgAbout.ShowModal;
  60. end;
  61. function TAboutProperty.GetAttributes: TPropertyAttributes;
  62. begin
  63.   Result := [paDialog, paReadOnly];
  64. end;
  65.  
  66. function TAboutProperty.GetValue: string;
  67. begin
  68.   Result := 'About Listbox Gadget...';
  69. end;
  70. {TItemsProperty Methods.}
  71. procedure TItemsProperty.Edit;
  72. var
  73.   dlgEditor: TListEditor;
  74.   objListBox: TBillListBox;
  75. begin
  76. try
  77.   objListBox := (self.GetComponent(0) as TBillListBox);
  78.   dlgEditor := TListEditor.Create(Application);
  79.   dlgEditor.SetReferenceTo(objListBox);
  80.   dlgEditor.lstItems.Clear;
  81.   dlgEditor.lstItems.Items.AddStrings(objListBox.Items);
  82.   dlgEditor.ShowModal;
  83. finally
  84.   dlgEditor.Free;
  85. end;
  86. end;
  87. function TItemsProperty.GetAttributes: TPropertyAttributes;
  88. begin
  89.   Result := [paDialog, paReadOnly];
  90. end;
  91. function TItemsProperty.GetValue: string;
  92. begin
  93.   Result := 'Click to add/modify items...';
  94. end;
  95. {TBillListBox Methods.}
  96. procedure TBillListBox.FindItemDlg(const SearchItem: string);
  97. begin
  98. try
  99.   dlgFind := TdlgFind.Create(self);
  100.   dlgFind.Top := self.Parent.Top + self.Top;
  101.   dlgFind.Left := self.Parent.Left + self.Left;
  102.   dlgFind.edtFind.Text := SearchItem;
  103.   dlgFind.edtFind.SelStart := 1;
  104.   dlgFind.edtFind.AutoSelect:= False;
  105.   dlgFind.ShowModal;
  106. finally
  107.   dlgFind.Free;
  108. end;
  109. end;
  110. function TBillListBox.SearchList(Item: PChar): Integer;
  111. var
  112.   ItemFound: LongInt;
  113. begin   ItemFound := SendMessage(Handle,
  114.                          LB_FINDSTRING, CurrentIndex, LongInt(Item));
  115.   if ItemFound = LB_ERR then
  116.       begin
  117.       CurrentIndex := 0;
  118.       MessageDlgPos('Item not Found.', mtWarning, [mbOK],
  119.                     0, dlgFind.Left + 75, dlgFind.Top);
  120.       end
  121.   else
  122.       ItemIndex := ItemFound;
  123.  
  124.   CurrentIndex := ItemFound;
  125.   Result := ItemFound;
  126. end;
  127. procedure TBillListBox.SetCurrentIndex(CurrIndex: LongInt);
  128. begin
  129. CurrentIndex := CurrIndex;
  130. end;
  131. procedure TBillListBox.KeyPress(var Key: Char);
  132. begin
  133. { Activate AutoSearch Dialog if the the AutoSearch property is set to True and
  134.   the Esc key and the Enter key have not been pressed. }
  135. if (AutoSearch) then
  136.    begin
  137.    if ( (Key <> Chr(27)) and (Key <> Chr(13)) ) then
  138.      begin
  139.      { Activate Listbox AutoSearch Dialog }
  140.      self.FindItemDlg(Key);
  141.      { Disable Listbox KeyPress event by setting Key := null }
  142.      Key := #0;
  143.      end;
  144.    end
  145. else
  146.    { Do default bahavior}
  147.    inherited KeyPress(Key);
  148. end;
  149. procedure Register;
  150. begin
  151.   RegisterComponents('Samples', [TBillListBox]);
  152.   RegisterPropertyEditor(TypeInfo(ShortInt), TBillListBox, 'About', 
  153.                                                               TAboutProperty);
  154.   RegisterPropertyEditor(TypeInfo(TStrings), TBillListBox, 'Items',
  155.                                                               TItemsProperty);
  156. end;
  157. initialization
  158.   CurrentIndex := 0;
  159. end.
  160.  
  161. Listing Two
  162. { * Unit Name: finddlg.pas
  163.   * Author:    William Stamatakis
  164.   * Note:      Search List Dialog for Multi-Char Listbox Component (TdlgFind)
  165. }
  166. unit Finddlg;
  167. interface
  168.  
  169. uses
  170.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  171.   Forms, Dialogs, StdCtrls, Buttons;
  172. type
  173.   TdlgFind = class(TForm)
  174.     edtFind: TEdit;
  175.     Label1: TLabel;
  176.     btnFindNext: TBitBtn;
  177.     btnClose: TBitBtn;
  178.     procedure ButtonsClick(Sender: TObject);
  179.   private
  180.     { Private declarations }
  181.   public
  182.     { Public declarations }
  183.  end;
  184. var
  185.   dlgFind: TdlgFind;
  186.  
  187. implementation
  188. uses Tbillist;
  189.  
  190. {$R *.DFM}
  191.  
  192. procedure TdlgFind.ButtonsClick(Sender: TObject);
  193. var
  194.   strFind: string;
  195.   pFind: PChar;
  196. begin
  197. if Sender = btnClose then
  198.   begin
  199.   (Owner as TBillListBox).SetCurrentIndex(0);
  200.   Close;
  201.   end
  202. else
  203.   begin
  204.   strFind := edtFind.Text;
  205.   pFind := @strFind;
  206.   StrPCopy(pFind, strFind);
  207.   (Owner as TBillListBox).SearchList(pFind);
  208.   end;
  209. end;
  210. end.
  211.  
  212.  
  213.  
  214.  
  215. Example 1:
  216.  
  217. (a)
  218. type TBillListBox = class(TListBox)
  219.  
  220. (b)
  221.  
  222. uses
  223.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  224.   Forms, Dialogs, StdCtrls, FindDlg, Buttons, DsgnIntf, About;
  225.  
  226. (c)
  227.  
  228. published
  229.     property AutoSearch: Boolean read FAutoSearch write FAutoSearch default False;
  230.  
  231.  
  232. (d)
  233.  
  234. protected
  235.     procedure KeyPress(var Key: Char); override;
  236.  ...
  237. procedure TBillListBox.KeyPress(var Key: Char);
  238. begin
  239. { Activate AutoSearch Dialog if the AutoSearch property is set to True and
  240.   the Esc key and the Enter key have not been pressed. }
  241. if (AutoSearch) then
  242.    begin
  243.    if ( (Key <> Chr(27)) and (Key <> Chr(13)) ) then
  244.      begin
  245.      { Activate Listbox AutoSearch Dialog }
  246.      self.FindItemDlg(Key);
  247.      { Disable Listbox KeyPress event by setting Key := null }
  248.      Key := #0;
  249.      end;
  250.    end
  251. else
  252.    { Do default bahavior}
  253.    inherited KeyPress(Key);
  254. end;
  255.  
  256. Example 2:
  257.  
  258. (a)
  259.  
  260. (Owner as TBillListBox).SearchList(pFind);
  261.  
  262.  
  263. (b)
  264.  
  265. ItemFound := SendMessage(Handle, LB_FINDSTRING, CurrentIndex, LongInt(Item));
  266.  
  267.  
  268. (c)
  269.  
  270. public
  271.     procedure FindItemDlg(const SearchItem: string);
  272.     procedure SearchList(Item: PChar);
  273.     procedure SetCurrentIndex( CurrIndex: LongInt);
  274.  
  275. Example 3:
  276.  
  277. (a)
  278. type
  279.   TItemsProperty = class(TPropertyEditor)
  280. public
  281.   procedure Edit; override;
  282.   function GetAttributes: TPropertyAttributes; override;
  283.   function GetValue: string; override;
  284. end;
  285.  
  286.  
  287. (b)
  288.  
  289. function TItemsProperty.GetAttributes: TPropertyAttributes;
  290. begin
  291.   Result := [paDialog, paReadOnly];
  292. end;
  293.  
  294. (c)
  295. function TItemsProperty.GetValue: string;
  296. begin
  297.   Result := 'Click to add/modify items...';
  298. end;
  299.  
  300. (d)
  301.  
  302. RegisterPropertyEditor(TypeInfo(TStrings), TBillListBox, 'Items',TItemsProperty);
  303.  
  304. (e)
  305.  
  306. RegisterComponents('Samples', [TBillListBox]);
  307.  
  308.